var kfid="";
var rkwclist = new Array();
$(document).ready(function(){

	getUserRight();
	//日期函数
	$('.dateInput').datetimepicker({
			format: 'yyyy-mm-dd',
	        forceParse: 0,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        minView: 2,
	        bootcssVer:3
	});	 
	$('.dateInput').datetimepicker('setStartDate', '1900-01-01');
	var endDate = new Date();
	$('.dateInput').datetimepicker('setEndDate', endDate);	
	$("input[name=rkqrsj]").val(nowDate());
	$("#functionNav").attr("href",navAddr);
	//获取并填充来源类型
	var lylxJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInJb.do",{'tableName':'jb_lyfs'});
	loadSelect("#form1 select[name=lylx]",lylxJson.rows,'id','name');
	//获取并填充时代
	getSdOption();
	//var doms_sd = ["DJH_SD1","DJH_SD2","DJH_SD3","DJH_SD4"];
	//selectFour(url_nd,doms_sd);
	
	//获取Url中的参数
	//var cpid =GetQueryString("id");
	kfid =GetQueryString("szkf");
	$("#form1 input[name=szkf]").val(kfid);
	//隐藏方位信息输入
	$(".fwxx").hide();
	//获取查询条件
	var params =$("#form1").serializeObject();
	//获取并填充方位类型
	var fwmsJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInXtjb.do",{'tableName':'xtjb_fwms'});
	loadSelect("#form_fw select[name=fwlx]",fwmsJson.rows,'id','name');
  	//获取并填充征集员列表
  	var zjJson = ajaxResult("get",false,ProjectName+"/root.getUserListByRole.do",{'roleId':'cangpinzhengjiyuan'});
	loadSelect("select[name=rkzjqrrid1],select[name=rkzjqrrid2]",zjJson.rows,'userid','username');
	//获取并填充总账管理员列表
  	var zzJson = ajaxResult("get",false,ProjectName+"/root.getUserListByRole.do",{'roleId':'zongzhangguanliyuan'});
	loadSelect("select[name=rkzzqrrid1],select[name=rkzzqrrid2]",zzJson.rows,'userid','username');
	/*//获取并填充库房责任保管员列表
  	var bgyJson = ajaxResult("get",false,ProjectName+"/sysmanage.getBgyByKfId.do",{'kfid':kfid});
	loadSelect("select[name=rkbgqrrid1],select[name=rkbgqrrid2]",bgyJson.rows,'bgyid','bgymc');	*/
	//获取并填充库房责任保管员列表
  	var bgyJson = ajaxResult("get",false,ProjectName+"/root.getUserListByOrg.do",{'orgId':userInfo.orgid});
  	if(bgyJson.status="ok"){
  		loadSelect("select[name=rkbgqrrid2]",bgyJson.rows,'userid','username');
	}else{
		alertFunc("获取保管员列表失败！");
		return false;
	}	
  	//获取并填充保管部门主任列表
  	if(getUserRight()){
  		var zrJson = ajaxResult("get",false,ProjectName+"/root.getDirectorList.do",{'orgId':userInfo.orgid,'pos':'主任'});
  		loadSelect("select[name=rkbgzrid]",zrJson.rows,'userid','username');	
	}	
	
	//初始化待入库Table
//  	createTable('#dbTable',ProjectName+'/colmanage.getDbCpList.do','id',tblColTitle,params);  
  	//点击查询按钮
	 $("#search_btn").click(function(){
		 refreshTable('#dbTable','#form1');
	 });
	//点击查询按钮
	 $("#clear_btn").click(function(){
		 $("#rkdj_btn").show();
		 $("#refpkf_btn").show();
		 $('#dbTable').bootstrapTable('destroy');	
		 var params =$("#form1").serializeObject();
		 createTable('#dbTable',ProjectName+'/colmanage.getDbCpList.do','id',tblColTitle,params);  
	 });
    //点击退回重新分配库房按钮
	 $("#refpkf_btn").click(function(){
		 /*var ids = new Array();
		 $("#dbTable input[name='btSelectItem']:checkbox").each(function(){ 
			 if ("checked" == $(this).attr("checked")) { 
				 ids.push($(this).attr('value'));
			 } 
		}); */
		var ids = "";
		 var ids=selDataIds('#dbTable');
		if(ids.length == 0){
			alertFunc("请选择记录！");
			return false;
		}
		bootbox.setLocale("zh_CN");
		bootbox.confirm("您确定该藏品不属于该库房，需要重新分配库房吗？",function(r){
			if(r){
				var dbResult = ajaxResult("post",false,ProjectName+"/colmanage.tijiaoDbFenpeiKf.do",{'ids':ids+""});
				if(dbResult["status"] == 'ok'){
				   alertFunc("回重新分配库房成功！");
				   refreshTable('#dbTable','#form1');
			    }else{
				   alert(dbResult["error"]);
			    }
			}
		});
	 });	
	 
	 //打印凭证
	 $("#dypz_btn").click(function(){
		 var params = "";
		 //var ztbz = $("input[name=ztbz]").val();
		 //if(ztbz==4){			 
//			 $("#dbTable input[name='btSelectItem']:checkbox").each(function(){ 
//				 if ("checked" == $(this).attr("checked")) { 
//					 ids+=$(this).attr('value')+",";
//				 } 
//			 }); 
/*			 params = "kfid="+kfid+"&userid="+userInfo.userid;
		 }else if(ztbz==5){*/
			 var ids = "";
			 var ids=selDataIds('#dbTable');
			/* $.each(rkwclist,function(index,value){
				 ids+=value+",";
			 });*/
			 if(ids.length == 0){
				 //alertFunc("请选择记录！");
				 //return false;
				 params = "kfid="+kfid+"&userid="+userInfo.userid;
			 }else{
				 params = "id="+ids;
			 }
			/* else
				 ids = ids.substring(0,ids.length-1);*/
		 //}

		 window.location.href=ProjectName+"/colmanage.createRkpz.do?"+params;
	 });
/*	 //点击入库确认按钮，打开入库窗口
	 $("#tjrk_btn").click(function(){
		 var ids = new Array();
		 var fwCheck = true;
		 $("#dbTable input[name='btSelectItem']:checkbox").each(function(){ 
			 if ("checked" == $(this).attr("checked")) { 
				 ids.push($(this).attr('value'));
				 if($(this).attr("fw")=="" || $(this).attr("fw")==null || $(this).attr("fw")==undefined){
					 fwCheck = false;
					 return false;
				 }
			 } 
		}); 	
		 if(!fwCheck){
				alertFunc("请录入方位后再进行入库！");
				return false;
			}
		if(ids.length == 0){
			alertFunc("请选择记录！");
			return false;
		}
		openRk(ids);
	 });*/
	 
	 //点击入库点交按钮，打开入库窗口
	 $("#rkdj_btn").click(function(){
		 //var ids = new Array();
		 var fwCheck = true;
		/* $("#dbTable input[name='btSelectItem']:checkbox").each(function(){ 
			 if ("checked" == $(this).attr("checked")) { 
				 ids.push($(this).attr('value'));
//				 if($(this).attr("fw")=="" || $(this).attr("fw")==null || $(this).attr("fw")==undefined){
//					 fwCheck = false;
//					 return false;
//				 }
			 } 
		}); 	*/
//		 if(!fwCheck){
//				alertFunc("请录入方位后再进行入库！");
//				return false;
//			}
//			var ids = "";
		 var ids=selDataIds('#dbTable');
		if(ids.length == 0){
			alertFunc("请选择记录！");
			return false;
		}
		if(ids!=null && ids!=""){
			if(getUserRight()){
				$("#form_rkdj")[0].reset();
				$("#form_rkdj input[name=ids]").val(ids);
				$("#form_rkdj #rkbgqrrmc1").val(userInfo.username);
				$("#form_rkdj #rkbgqrrid1").val(userInfo.userid);	
				$("input[name=rkqrsj]").val(nowDate());
				$("#rkdjModal").modal('show');
			}
		}
	 });
	 
/*	 //点击模态框中的入库按钮
	 $('#rk_btn').click(function(){
		 if(checkform("#form_rk")){
			   var params =$("#form_rk").serializeObject();
			   params["rkzjqrrmc1"] =$("#form_rk select[name=rkzjqrrid1]").find("option:selected").text();
			   if(params["rkzjqrrid2"]!="")
				   params["rkzjqrrmc2"] =$("#form_rk select[name=rkzjqrrid2]").find("option:selected").text();
			   params["rkzzqrrmc1"] =$("#form_rk select[name=rkzzqrrid1]").find("option:selected").text();
			   if(params["rkzzqrrid2"]!="")
				   params["rkzzqrrmc2"] =$("#form_rk select[name=rkzzqrrid2]").find("option:selected").text();
			   params["rkbgqrrmc1"] =$("#form_rk select[name=rkbgqrrid1]").find("option:selected").text();
			   if(params["rkbgqrrid2"]!="")
				   params["rkbgqrrmc2"] =$("#form_rk select[name=rkbgqrrid2]").find("option:selected").text();
			   params["rkbgzrmc"] =$("#form_rk select[name=rkbgzrid]").find("option:selected").text();
			   var result="";
			   if(params.ids != null || params.ids != ""){
				  rkqrxx = ajaxResult("get",false,ProjectName+'/colmanage.rukurenXinxi.do',params);
				  if(rkqrxx["status"] == 'ok'){
					   result = ajaxResult("post",false,ProjectName+'/colmanage.tijiaoDbRuku.do',params);
					   if(result["status"] == 'ok'){
						   alertFunc("提交入库成功！");
						   $('#rkModal').modal('hide');
						   refreshTable('#dbTable','#form1');
					   }else{
						   alertFunc(result["error"]);
					   }
				   }else{
					   alertFunc(rkqrxx["error"]);
				   }
			   }
			}
	 });*/
	 
	//点击入库点交模态框中的入库点交按钮
	 $('#rkdj_add_btn').click(function(){		 
		 if(checkform("#form_rkdj")){
			 if($("#form_rkdj input[name=rkbgqrrid1]").val() == $("#form_rkdj select[name=rkbgqrrid2]").val()){
				   alertFunc("库房责任保管员不能重复");
				   return false;
			   }
			 if($("#form_rkdj select[name=rkzjqrrid1]").val() == $("#form_rkdj select[name=rkzjqrrid2]").val()){
				   alertFunc("藏品征集员不能重复");
				   return false;
			   }
			   if($("#form_rkdj select[name=rkzzqrrid1]").val() == $("#form_rkdj select[name=rkzzqrrid2]").val()){
				   alertFunc("总账管理员不能重复");
				   return false;
			   }			   
//			 var check= true;
//			    $("#form_rkdj input[id$=Check]").each(function(){
//					if($(this).val()=='0'){
//						alertFunc("用户验证未通过！");
//						check = false;
//						return false;
//					}
//				});
//			    if(!check)
//			    	return false;	
			    
			   var params =$("#form_rkdj").serializeObject();
			   params["rkzjqrrmc1"] =$("#form_rkdj select[name=rkzjqrrid1]").find("option:selected").text();
			   params["rkzjqrrmc2"] =$("#form_rkdj select[name=rkzjqrrid2]").find("option:selected").text();
			   params["rkzzqrrmc1"] =$("#form_rkdj select[name=rkzzqrrid1]").find("option:selected").text();
			   params["rkzzqrrmc2"] =$("#form_rkdj select[name=rkzzqrrid2]").find("option:selected").text();
			   params["rkbgqrrmc2"] =$("#form_rkdj select[name=rkbgqrrid2]").find("option:selected").text();
			   params["rkbgzrmc"] =$("#form_rkdj select[name=rkbgzrid]").find("option:selected").text();
			   var result="";
			   if(params.ids != null || params.ids != ""){
				  rkqrxx = ajaxResult("get",false,ProjectName+'/colmanage.rukurenXinxi.do',params);
				  if(rkqrxx["status"] == 'ok'){
					   result = ajaxResult("post",false,ProjectName+'/colmanage.tijiaoDbRuku.do',params);
					   if(result["status"] == 'ok'){
						   alertFunc("入库点交成功！");
						   $('#rkdjModal').modal('hide');
						   refreshTable('#dbTable','#form1');
					   }else{
						   alertFunc(result["error"]);
					   }
				   }else{
					   alertFunc(rkqrxx["error"]);
				   }
			   }
			}
	 });
 
	 //选择方位描述时，切换显示方位输入框
	 $("select[name=fwlx]").change(function(){
		 var fwlxid = $(this).val();
		 var fwlxmc = $("select[name=fwlx]").find("option:selected").text();
		 if(fwlxid!=null && fwlxid!=""){
			 var fwlxArr = fwlxmc.split('-');
			 $(".fwxx").hide();
			 for(i=0;i<fwlxArr.length;i++){
				 $("label[name=fw"+(i+1)+"lbl]").text(fwlxArr[i]);
				 $("input[name=fw"+(i+1)+"],label[name=fw"+(i+1)+"lbl]").show();
			 }			 
		 }else{
			 $(".fwxx").hide();
		 }
		 $("#fwxxDiv input[name^=fw]").each(function(){
			 if($(this).css('display')=="none"){
				 $(this).val("");
			 }
		 });
		 $("#fwxxDiv label[name^=fw]").each(function(){
			 if($(this).css('display')=="none"){
				 $(this).val("");
			 }
		 });		 
	 });
	 //提交方位信息
	 $("#fw_btn").click(function(){
		var fw1=$('#form_fw input[name=fw1]').val();
		var fw1lbl=$("label[name=fw1lbl]").text();
		var fw2=$('#form_fw input[name=fw2]').val();
		var fw2lbl=$("label[name=fw2lbl]").text();
		var fw3=$('#form_fw input[name=fw3]').val();
		var fw3lbl=$("label[name=fw3lbl]").text();
		var fwxx='';	//方位信息
		var fw='';		//方位
		
		/*var flh = $('#form_fw input[name=flh]').val();
		if(flh==''){
			alertFunc('请输入分类号');
			return;
		}*/
		if(fw1!=null && fw1!=''){
			fwxx += fw1;
			fw += fw1+fw1lbl;
		}				
		if(fw2!=null && fw2!=''){
			fwxx += '-'+fw2;
			fw += fw2+fw2lbl;
		}				
		if(fw3!=null && fw3!=''){
			fwxx += '-'+fw3;
			fw += fw3+fw3lbl;
		}		
		if(checkform("#form_fw")){
			var params =$("#form_fw").serializeObject();
			if($("select[name=fwlx]").val()==''){
				delete params["fwlx"];
			}else{
				params["fwxx"]=fwxx;
				params["fw"]=fw;
			}
			if(params.id != null || params.id != ""){
				result = ajaxResult("post",false,ProjectName+'/colmanage.updateDbFw.do',params);
				if(result["status"] == 'ok'){
					alertFunc("编辑方位信息成功！");
					$('#fwModal').modal('hide');
					refreshTable('#dbTable','#form1');
				}else{
					alertFunc(result["error"]);
				}
			}
		}
	 });
	 $("select[name=ztbz]").change(function(){
		 var ztbz = $(this).val();
		 inittable(ztbz);
	 });
	 inittable( $("select[name=ztbz]").val());
})

function inittable(ztbz){
	if(ztbz==4){
		$("#rkdj_btn").show();
		$("#refpkf_btn").show();
		var params =$("#form1").serializeObject();
		$('#dbTable').bootstrapTable('destroy');	
		createTable('#dbTable',ProjectName+'/colmanage.getDbCpList.do','id',tblColTitle,params);  
	}else{
		$("#rkdj_btn").hide();
		$("#refpkf_btn").hide();
		var rkwcstr = $("#rkwclist").val();
		if(rkwcstr!=""){			
			$.each(rkwcstr.split(","),function(index,value){
				rkwclist.push(value);
			});
		}
		var params =$("#form1").serializeObject();
		params["rkbgqrrid1"] = userInfo.userid;
		$('#dbTable').bootstrapTable('destroy');	
		createTable('#dbTable',ProjectName+'/colmanage.getDbCpList.do','id',tblColTitle5,params);  
	}
	
}
//打开入库窗口
function openRk(ids){
	if(ids!=null && ids!=""){
		$("#form_rk input[name=ids]").val(ids);
		$('#rkModal').modal('show');
	}
}

//打开方位窗口
function openFw(data){
	$("#form_fw input[name=id]").val(data.id);
	result = ajaxResult("get",false,ProjectName+'/colmanage.getDbFw.do',{"id":data.id});
	if(result["status"] == 'ok'){	
		loadData("#form_fw",result.data); 
		if(result.data.fwxx!="" && result.data.fwxx!=null){
			fwxx=result.data.fwxx.split("-");
			for(i=0;i<fwxx.length;i++){
				$("#form_fw input[name=fw"+(i+1)+"]").val(fwxx[i]);
			}
		}
		$("select[name=fwlx]").change();
		$('#fwModal').modal('show'); 
	}else{
		alertFunc(result["error"]);
	}
}

function checkdjr(djr1,djr2){
	var djr1value=$("select[name="+djr1+"]").val();
	var djr2value=$("select[name="+djr2+"]").val();
	if(djr1value==djr2value){
		$("select[name="+djr2+"]").next("span").remove();
		$("select[name="+djr2+"]").after("<span class='apendhtml' style='color: red;margin-left: 5px;'>点交人不能重复</span>");
	}else{
		$("select[name="+djr1+"]").next("span").remove();
		$("select[name="+djr2+"]").next("span").remove();
	}
}

//验证用户名密码是否有效
function validateUser(formid,input1,input2){
	var userid = $(formid+" "+input1).val();
	var password = $(formid+" "+input2).val();
	if(userid!=''){
		var params = new Object();
		params["userid"] = userid;	
		params["password"] = password;
		var result = ajaxResult("get",false,ProjectName+'/root.validateUser.do',params);
		if(result.status == "ok"){
			$(formid+" "+input2).next("span").remove();
			$(formid+" "+input2).after("<span name='validateText' style='margin-left:5px;'>通过</span>");
			$(formid+" "+input1+"Check").val("1");
		}else{
			$(formid+" "+input2).next("span").remove();
			$(formid+" "+input2).after("<span name='validateText' style='color:red;margin-left:5px;'>不通过</span>");
			$(formid+" "+input1+"Check").val("0");
		}
	}
}

function Addrkwclist(obj,id){
	if(!obj.checked){
		rkwclist.pop(id);
		$("#rkwclist").val(rkwclist.toString());
	}else{
		rkwclist.push(id);
		$("#rkwclist").val(rkwclist.toString());
	}
}
//判断点交用户是否有效
function checkuser(fromid,useridInput,isValidateInput){
	var userid= $(formid+" "+useridInput).val();
	var isValidate= parseInt($(formid+" "+isValidateInput).val());
	var result = false;
	if(userid == ""){
		return true;
	}else{
		return isValidate;
	}
}

//更新分类号
function submitFlh(flhobj){
	var params = new Object();
	params["id"] = $(flhobj).attr('id');	
	params["flh"] = $(flhobj).val();
	if(params.id != null || params.id != ""){
		result = ajaxResult("post",false,ProjectName+'/colmanage.updateDbFlh.do',params);
		if(result["status"] == 'ok'){
			//refreshTable('#dbTable','#form1');
			var params =$(formid).serializeObject();	 
			 params["limit"] = pageSize;
			 $(tableid).bootstrapTable('refresh',{query:params});
		}else{
			alertFunc(result["error"]);
		}
	}
}

var tblColTitle =  [{
	field: 'check',
	 checkbox:true	 
   },  {
        field: 'zdjh',
        title: '总登记号'
    },{
	        field: 'cpmc',
	        title: '藏品名称',
	        formatter: function(value, row, index) {	
	        	var result="";	  
    			//if(row.sdbz==1 && row.sdrid == userInfo.userid)	   
        			result = '<a href="dbDetail.html?id='+row.id+'">'+value+'</a>';
    			//else
    			//	result =value;
	        	return result;		        	
		      }
	    }, {
	        field: 'sd4',
	        title: '时代',
	        formatter: function(value, row, index) {
	        	var sd="";
	        	if(row.sd4!="" && row.sd4!=null){
	        		sd =row.sd4;
	        	}else if(row.sd3!="" && row.sd3!=null){
	        		sd =row.sd3;
	        	}else if(row.sd2!="" && row.sd2!=null){
	        		sd =row.sd2;
	        	}else if(row.sd1!="" && row.sd1!=null){
	        		sd =row.sd1;
	        	}
	        	return sd;
		      }
	    }, {
	        field: 'sjsl',
	        title: '实际数量',
	        formatter: function(value, row, index) {	        	
	        	return value+row.sjsldw;
		      }
	    }, {
	        field: 'lylxmc',
	        title: '来源方式'
	    }, {
	        field: 'szkf',
	        title: '库房'
	    },{
	        field: 'fw',
	        title: '方位'
	    }/*,{
	        field: 'sdrmc',
	        title: '锁定人',
	        formatter: function(value, row, index) {
	        	  if(row.sdbz==1)
	        		  return value;
	        	  else
	        		  return "";
		      	}
	    }*/,{
	        field: 'flh',
	        title: '分类号',
	        width:100,
	        edit:{type:'text' },
	        formatter: function(value, row, index) {
	        	var result="";	  
    			//if(row.sdbz==1 && row.sdrid == userInfo.userid)	   
    				result = "<input type='text' style='width:100px' class='form-control input-sm' value='"+row.flh+"' id='"+row.id+"' onblur='javascript:submitFlh(this)'/>";
    			//else
    			//	result=row.flh;
	        	return result;
		    }	        
	    },{
	        field: '',
	        title: '操作',
	        formatter: function(value, row, index) {
	        	var result="";	  
    			//if(row.sdbz==1 && row.sdrid == userInfo.userid)	   
    				result = "<a href='rkInfo.html?id="+row.id+"&szkf="+kfid+"'>完残情况</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' onclick='openFw("+JSON.stringify(row)+")'>方位信息</a>";
	        	return result;
		      }
	    }];

var tblColTitle5 = [{
	 field: 'id',
	 title: '选择',
	 class:'bs-checkbox',
	 formatter: function(value, row, index) {
		 var result ='';var flag = false;
		 $.each(rkwclist,function(index,value){
			 if(value==row.id){		
				 flag = true;
				 //break;
			 }
		 });
		 if(flag)
			 result = '<input data-index="'+index+'" name="btSelectItem" checked type="checkbox" value="'+row.id+'" onclick="Addrkwclist(this,'+row.id+')">';
		 else
			 result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" onclick="Addrkwclist(this,'+row.id+')">';	 
  	  	return result;
  	  }
   }, {
       field: 'zdjh',
       title: '总登记号'
   },{
	        field: 'cpmc',
	        title: '藏品名称',
	        formatter: function(value, row, index) {	
	        	var result="";	  
        		result = '<a href="dbDetail.html?id='+row.id+'">'+value+'</a>';
	        	return result;		        	
		      }
	    }, {
	        field: 'sd4',
	        title: '时代',
	        formatter: function(value, row, index) {
	        	var sd="";
	        	if(row.sd4!="" && row.sd4!=null){
	        		sd =row.sd4;
	        	}else if(row.sd3!="" && row.sd3!=null){
	        		sd =row.sd3;
	        	}else if(row.sd2!="" && row.sd2!=null){
	        		sd =row.sd2;
	        	}else if(row.sd1!="" && row.sd1!=null){
	        		sd =row.sd1;
	        	}
	        	return sd;
		      }
	    }, {
	        field: 'sjsl',
	        title: '实际数量',
	        formatter: function(value, row, index) {	        	
	        	return value+row.sjsldw;
		      }
	    }, {
	        field: 'lylxmc',
	        title: '来源方式'
	    }, {
	        field: 'szkf',
	        title: '库房'
	    },{
	        field: 'fw',
	        title: '方位'
	    },{
	        field: 'flh',
	        title: '分类号',
	        width:100
	    }];